package com.shd.edanpei.presenter;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.RegisterResultActivity;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.model.NoDataResponse;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.PerfectDataBean;
import com.shd.edanpei.presenter.contract.PerfectDataContract;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.util.GsonUtil;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;


public class PerfectDataPresenter extends BasePresenterImpl<PerfectDataContract.PerfectDataIView>
        implements PerfectDataContract.PerfectDataIPresenter {

    private PerfectDataBean bean = new PerfectDataBean();
    //提交按钮是否可点击
    private boolean canSubmit = false;

    public PerfectDataPresenter(PerfectDataContract.PerfectDataIView mView) {
        super(mView);
    }

    @Override
    public void setCompanyName(String str, int roleType) {
        bean.setCompanyName(str);
        checkBtn(roleType);
    }
//
//    @Override
//    public void setBelongStation(String str, int roleType) {
//        bean.setEmployeeNo(str);
//        checkBtn(roleType);
//    }
//
//    @Override
//    public void setWorkNo(String str, int roleType) {
//        bean.setEmployeeNo(str);
//        checkBtn(roleType);
//    }

    @Override
    public void setEnterArea(final TextView text, final int roleType) {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {
            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                mBaseIView.setText(text, address);
                bean.setArea(address);
                checkBtn(roleType);
            }
        });
    }

    @Override
    public void setCompanyAddress(final TextView text, final int roleType) {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {
            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                mBaseIView.setText(text, address);
                bean.setCompanyAddress(address);
                checkBtn(roleType);
            }
        });
    }

    @Override
    public void setDetailedAddress(String str, int roleType) {
        bean.setDetailAddress(str);
        checkBtn(roleType);
    }

    @Override
    public void submit(int roleType) {
        if (canSubmit) {
            mBaseIView.showLoading("");
            HashMap<String, Object> map = GsonUtil.toMap(bean);
            ApiCreator.getApi().perfectInfo(map)
                    .compose(RxTransformer.<NoDataResponse>errorHandle())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseObserver<NoDataResponse>() {
                        @Override
                        protected void onSuccess(NoDataResponse data) {
                            DaoManager.getInstance().setIsPerfect(true);
                            //提交之后进入等待认证页面
                            Bundle bundle = new Bundle();
                            bundle.putInt(Constant.TYPE, RegisterResultActivity.apply);
                            mBaseIView.startActivity(RegisterResultActivity.class, bundle);
                            mBaseIView.getContext().finish();
                        }

                        @Override
                        public void onFinish() {
                            mBaseIView.stopLoading();
                        }
                    });


        }
    }

    @Override
    public void onCompanySize(final TextView tvCompanySize, final int roleType) {
        DialogUtil.listDialog(mBaseIView.getContext(), "公司规模", R.array.company_size, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                bean.setCompanySize(data);
                mBaseIView.setText(tvCompanySize, data);
                checkBtn(roleType);
            }
        });

    }

    @Override
    public void onWorkArea(final TextView tvWorkArea, final int roleType) {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {

            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                bean.setCompanyAddress(address);
                mBaseIView.setText(tvWorkArea, address);
                checkBtn(roleType);
            }
        });
    }

    @Override
    public void setBusinessLicense(String businessCert, int roleType) {
        bean.setBusinessLicense(businessCert);
        checkBtn(roleType);
    }

    @Override
    public void setBranch(String string, int roleType) {
        bean.setBranch(string);
        checkBtn(roleType);
    }

    @Override
    public void setBankAddress(final TextView tvBankAddress, final int roleType) {
        DialogUtil.addressPick(mBaseIView.getContext(), new DialogUtil.AddressListener() {

            @Override
            public void done(String address, String provinceCode, String cityCode, String districtCode) {
                bean.setCompanyAddress(address);
                mBaseIView.setText(tvBankAddress, address);
                checkBtn(roleType);
            }
        });
    }

    //验证按钮是否可点击，并改变样式
    private void checkBtn(int roleType) {

        if (roleType == 2) {
            //银行经理
            if (TextUtils.isEmpty(bean.getCompanyName())
                    || TextUtils.isEmpty(bean.getBranch())
//                    || TextUtils.isEmpty(bean.getEmployeeNo())
//                    || TextUtils.isEmpty(bean.getArea())
                    || TextUtils.isEmpty(bean.getCompanyAddress())
                    || TextUtils.isEmpty(bean.getDetailAddress())) {
                //提交按钮不可点击
                canSubmit = false;
                mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_btn_unclick));
                return;
            }
            //按钮可点击
            canSubmit = true;
            mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_main_button));
        } else {
            if (TextUtils.isEmpty(bean.getCompanyName())
                    || TextUtils.isEmpty(bean.getBusinessLicense())
                    || TextUtils.isEmpty(bean.getCompanySize())
                    || TextUtils.isEmpty(bean.getCompanyAddress())
                    || TextUtils.isEmpty(bean.getDetailAddress())) {
                //提交按钮不可点击
                canSubmit = false;
                mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_btn_unclick));
                return;
            }
            //按钮可点击
            canSubmit = true;
            mBaseIView.getSubmitView().setBackground(ContextCompat.getDrawable(mBaseIView.getContext(), R.drawable.shape_main_button));
        }


    }
}
